import 'package:flutter/material.dart';
import 'package:learn/view/index.dart';
import 'package:fluro/fluro.dart';
import 'package:learn/model/routes.dart';

void main() {
  final router = Router();
  Routes.configureRoutes(router);
  Routes.router = router;

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // showPerformanceOverlay: true,
//      onGenerateRoute: Routes.router.generator,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Helloworld'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {

  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            backgroundColor: Colors.white,
            title: Text(widget.title, style: TextStyle(color: Colors.black)),
            centerTitle: true),
        bottomNavigationBar: BottomBar(
          callback: (index) => _tabController.index = index,
        ),
        body: TabBarView(
          controller: _tabController,
          children: <Widget>[
            IndexList(),
            Center(
              child: Text(
                '我的',
                style: TextStyle(color: Colors.blue),
              ),
            )
          ],
          physics: NeverScrollableScrollPhysics(),
        ));
  }
}

class BottomBar extends StatefulWidget {
  final Function(int index) callback;

  const BottomBar({Key key, this.callback}) : super(key: key);
  @override
  _BottomBarState createState() => _BottomBarState();
}

class _BottomBarState extends State<BottomBar> {
  int _pageIndex = 0;

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
        currentIndex: _pageIndex,
        onTap: (index) {
          setState(() {
            _pageIndex = index;
          });
          widget.callback(_pageIndex);
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
          BottomNavigationBarItem(icon: Icon(Icons.people), title: Text('我的')),
        ]);
  }
}
